package fr.inserm.u915.organifooyi.lang;

import java.lang.reflect.Constructor;

public class Cast {

public static <T> boolean isA(String text,Class<T> clazz)
	{
	return cast(text,clazz)!=null;
	}
	
@SuppressWarnings("unchecked")
public static <T> T cast(String text,Class<T> clazz)
	{
	try {
		if(clazz.isEnum())
			{
			for(T o:clazz.getEnumConstants())
				{
				Enum<?> e=(Enum<?>)o;
				if(e.name().equals(text)) return clazz.cast(e);
				}
			return null;
			}
		else if( clazz.equals(Character.class))
			{
			if(text.length()!=1) return null;
			return (T)new Character(text.charAt(0));
			}
		Constructor<T> cstor=clazz.getConstructor(String.class);
		return cstor.newInstance(text);
		}
	catch (Exception e)
		{
		return null;
		}
	
	}
}
